import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;

public class MtBullerAdmin extends JFrame {
    private MtBullerResort resort;

    public MtBullerAdmin() {
        resort = new MtBullerResort();
        initUI();
    }

    private void initUI() {
        setTitle("Mt Buller Resort Admin");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1));
        JButton displayAllAccommodationsButton = new JButton("Display All Accommodations");
        JButton displayAvailableAccommodationsButton = new JButton("Display Available Accommodations");

        JButton addCustomerButton = new JButton("Add Customer");
        JButton createPackageButton = new JButton("Create Package");
        JButton listCustomersButton = new JButton("List Customers");
        JButton listPackagesButton = new JButton("List Packages");
        JButton addLiftPassButton = new JButton("Add Lift Pass to Package");
        JButton addLessonsButton = new JButton("Add Lessons to Package");
        JButton savePackagesButton = new JButton("Save Packages to File");
        JButton readPackagesButton = new JButton("Read Packages from File");
        JButton quitButton = new JButton("Quit");

        addCustomerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addCustomer();
            }
        });

        createPackageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createPackage();
            }
        });

        listCustomersButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listCustomers();
            }
        });

        listPackagesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listPackages();
            }
        });

        addLiftPassButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addLiftPassToPackage();
            }
        });

        addLessonsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addLessonsToPackage();
            }
        });
        displayAllAccommodationsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayAllAccommodations();
            }
        });

        displayAvailableAccommodationsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayAvailableAccommodations();
            }
        });

        savePackagesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                savePackagesToFile();
            }
        });

        readPackagesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                readPackagesFromFile();
            }
        });

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        panel.add(displayAllAccommodationsButton);
        panel.add(displayAvailableAccommodationsButton);
        panel.add(addCustomerButton);
        panel.add(createPackageButton);
        panel.add(listCustomersButton);
        panel.add(listPackagesButton);
        panel.add(addLiftPassButton);
        panel.add(addLessonsButton);
        panel.add(savePackagesButton);
        panel.add(readPackagesButton);
        panel.add(quitButton);
        add(panel);
    }
    private void displayAllAccommodations() {
        ArrayList<Accommodation> accommodations = resort.getAllAccommodations();
        StringBuilder message = new StringBuilder("All Accommodations:\n");
        for (Accommodation accommodation : accommodations) {
            message.append(accommodation).append("\n");
        }
        JOptionPane.showMessageDialog(this, message.toString());
    }
    private void displayAvailableAccommodations() {
        ArrayList<Accommodation> accommodations = resort.getAvailableAccommodations();
        StringBuilder message = new StringBuilder("Available Accommodations:\n");
        for (Accommodation accommodation : accommodations) {
            message.append(accommodation).append("\n");
        }
        JOptionPane.showMessageDialog(this, message.toString());
    }
    private void addCustomer() {
        JTextField idField = new JTextField();
        JTextField nameField = new JTextField();
        String[] levels = {"beginner", "intermediate", "expert"};
        JComboBox<String> levelBox = new JComboBox<>(levels);

        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("ID:"));
        panel.add(idField);
        panel.add(new JLabel("Name:"));
        panel.add(nameField);
        panel.add(new JLabel("Level:"));
        panel.add(levelBox);

        int result = JOptionPane.showConfirmDialog(null, panel, "Add Customer",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            int id = Integer.parseInt(idField.getText());
            String name = nameField.getText();
            String level = (String) levelBox.getSelectedItem();
            resort.addCustomer(new Customer(id, name, level));
            JOptionPane.showMessageDialog(this, "Customer added successfully.");
        }
    }

    private void createPackage() {
        JTextField customerIdField = new JTextField();
        JTextField startDateField = new JTextField();
        JTextField durationField = new JTextField();

        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("Customer ID:"));
        panel.add(customerIdField);
        panel.add(new JLabel("Start Date (YYYY-MM-DD):"));
        panel.add(startDateField);
        panel.add(new JLabel("Duration (days):"));
        panel.add(durationField);

        int result = JOptionPane.showConfirmDialog(null, panel, "Create Package",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            int customerId = Integer.parseInt(customerIdField.getText());
            String startDate = startDateField.getText();
            int duration = Integer.parseInt(durationField.getText());

            Accommodation accommodation = selectAccommodation();
            if (accommodation != null) {
                resort.addPackage(customerId, startDate, duration, accommodation.getId());
                JOptionPane.showMessageDialog(this, "Package created successfully.");
            } else {
                JOptionPane.showMessageDialog(this, "No available accommodations.");
            }
        }
    }
    private Accommodation selectAccommodation() {
        ArrayList<Accommodation> accommodations = resort.getAvailableAccommodations();
        if (accommodations.isEmpty()) {
            JOptionPane.showMessageDialog(this, "No available accommodations.");
            return null;
        }

        String[] options = new String[accommodations.size()];
        for (int i = 0; i < accommodations.size(); i++) {
            options[i] = accommodations.get(i).toString();
        }

        String selectedValue = (String) JOptionPane.showInputDialog(this, "Select Accommodation",
                "Accommodation Selection", JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

        if (selectedValue != null) {
            for (Accommodation accommodation : accommodations) {
                if (selectedValue.equals(accommodation.toString())) {
                    return accommodation;
                }
            }
        }
        return null;
    }
    private void listCustomers() {
        ArrayList<Customer> customers = resort.getCustomers();
        StringBuilder message = new StringBuilder("Customers:\n");
        for (Customer customer : customers) {
            message.append(customer).append("\n");
        }
        JOptionPane.showMessageDialog(this, message.toString());
    }

    private void listPackages() {
        ArrayList<TravelPackage> packages = resort.getPackages();
        StringBuilder message = new StringBuilder("Packages:\n");
        for (TravelPackage travelPackage : packages) {
            message.append(travelPackage).append("\n");
        }
        JOptionPane.showMessageDialog(this, message.toString());
    }

    private void addLiftPassToPackage() {
        JTextField packageIdField = new JTextField();
        JTextField countField = new JTextField();

        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("Package ID:"));
        panel.add(packageIdField);
        panel.add(new JLabel("Number of Lift Passes:"));
        panel.add(countField);

        int result = JOptionPane.showConfirmDialog(null, panel, "Add Lift Pass to Package",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            int packageId = Integer.parseInt(packageIdField.getText());
            int count = Integer.parseInt(countField.getText());
            resort.addLiftPassToPackage(packageId, count);
            JOptionPane.showMessageDialog(this, "Lift pass added successfully.");
        }
    }

    private void addLessonsToPackage() {
        JTextField packageIdField = new JTextField();
        JTextField countField = new JTextField();

        JPanel panel = new JPanel(new GridLayout(0, 1));
        panel.add(new JLabel("Package ID:"));
        panel.add(packageIdField);
        panel.add(new JLabel("Number of Lessons:"));
        panel.add(countField);

        int result = JOptionPane.showConfirmDialog(null, panel, "Add Lessons to Package",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            int packageId = Integer.parseInt(packageIdField.getText());
            int count = Integer.parseInt(countField.getText());
            resort.addLessonsToPackage(packageId, count);
            JOptionPane.showMessageDialog(this, "Lessons added successfully.");
        }
    }
    private void readPackagesFromFile() {
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
                ArrayList<TravelPackage> packages = (ArrayList<TravelPackage>) inputStream.readObject();
                for (TravelPackage travelPackage : packages) {
                    resort.addPackage(travelPackage.getCustomerId(), travelPackage.getDate(),
                            travelPackage.getDuration(), travelPackage.getAccommodation().getId());
                }
                JOptionPane.showMessageDialog(this, "Packages loaded successfully.");
            } catch (IOException | ClassNotFoundException e) {
                JOptionPane.showMessageDialog(this, "Error reading packages from file: " + e.getMessage());
            }
        }
    }

    private void savePackagesToFile() {
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showSaveDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file))) {
                outputStream.writeObject(resort.getPackages());
                JOptionPane.showMessageDialog(this, "Packages saved successfully.");
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "Error saving packages to file: " + e.getMessage());
            }
        }
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            MtBullerAdmin ex = new MtBullerAdmin();
            ex.setVisible(true);
        });
    }
}
